﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

using Infrastructure.Utility;
using SMG.Monitor.Domain.Models;

using SMG.ServiceContract.Program;
using SMG.Application.DTO.Program;
using Infrastructure.Core.IOC;

namespace SMG.Monitor.Domain.Services
{
    /// <summary>
    /// 节目单监视服务
    /// </summary>
    public class ProgramService : MonitorService
    {
        //节目单服务
        IProgramBillService billService = ServiceLocator.Instance.Resolve<IProgramBillService>();

        #region Ctors

        public ProgramService()
            : this(new MonitorSetting())
        { }
        public ProgramService(MonitorSetting monitorSetting)
            : base(monitorSetting)
        { }

        #endregion

        #region override MonitorService Members

        /// <summary>
        /// 文件创建处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnFileCreatedHandler(object sender, FileSystemEventArgs e)
        {
            try
            {
                var fileName = e.Name;
                var filePath = e.FullPath;
                if (!File.Exists(filePath))
                    throw new FileNotFoundException(String.Format("文件{0}不存在", filePath));

                var content = File.ReadAllText(filePath);
                var broadcast = SerializeUtility.XmlDeserialize<BroadcastList>(content);
                if (broadcast == null)
                    throw new Exception(String.Format("无效的节目单文件:{0}", content));

                //插入一条节目单版本记录
                var billVersionDto = new BillVersionRecordDto()
                {
                    ChannelId = broadcast.ChannelId,
                    PlayDate = broadcast.PlayDate,
                    BillName = broadcast.ListName,
                    BillVersion = broadcast.Version,
                    Subtitles = broadcast.Subtitles,
                    CreateTime = broadcast.CreateTime,
                    Creator = broadcast.Creator,
                    EditTime = broadcast.EditTime,
                    Editor = broadcast.Editor,
                    AuditTime = broadcast.AuditTime,
                    Auditor = broadcast.Auditor,
                };

                var billDtos = new List<ProgramBillDto>();
                //插入节目单列表
                if (broadcast.ListItem != null && broadcast.ListItem.Any())
                {
                    foreach (var item in broadcast.ListItem)
                    {
                        var billDto = new ProgramBillDto()
                        {
                            #region 节目单主信息
                            ChannelId = broadcast.ChannelId,
                            PlayDate = broadcast.PlayDate,
                            BillName = broadcast.ListName,
                            BillVersion = broadcast.Version,
                            Subtitles = broadcast.Subtitles,
                            CreateTime = broadcast.CreateTime,
                            Creator = broadcast.Creator,
                            EditTime = broadcast.EditTime,
                            Editor = broadcast.Editor,
                            AuditTime = broadcast.AuditTime,
                            Auditor = broadcast.Auditor,
                            #endregion

                            #region 节目单详细信息
                            ItemId = item.ItemId,
                            ItemIndex = item.ItemIndex,
                            ProgramTitle = item.ProgramTitle,
                            PlayTime = item.PlayTime,
                            ItemType = item.ItemType,
                            ProgramId = item.ProgramId,
                            InPoint = item.InPoint,
                            OutPoint = item.OutPoint,
                            Duration = item.Duration,
                            TapeId = item.TapeId,
                            TapeType = item.TapeType,
                            PlayDeviceId = item.PlayDeviceId,
                            StudioName = item.StudioName,
                            PlayPattern = item.PlayPattern,
                            ProgramSource = item.ProgramSource,
                            TapeInPoint = item.TapeInPoint,
                            TapeOutPoint = item.TapeOutPoint,
                            PlayMode = item.PlayMode,
                            ProgramType = item.ProgramType,
                            ReplaySymbol = item.ReplaySymbol,
                            ProgramLibMode = item.ProgramLibMode,
                            Afd = item.Afd,
                            IsHD = item.IsHD,
                            Notation = item.Notation,
                            CueTone = item.CueTone
                            #endregion
                        };
                        billDtos.Add(billDto);
                    }
                }
                //保存
                var result = billService.InsertBatchProgramBill(billVersionDto, billDtos);
            }
            catch (Exception ex)
            {

            }
            finally { 
                
            }
        }
        
        #endregion

    }
}
